﻿/******************************************************************************
* File description: NetworkConnect.qml
* Function: 网络连接页面
* Current Version: 1.1
* Author: 陈乃瑞
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.0
import QtQuick.Window 2.12
import "../components"
Rectangle {
    property int connectType: 0
    property bool isClicked: false //点击事件触发
    property bool isClicked2: false //端口的点击事件（照楼上抄的

    radius: 5*dp

    Connections{
        target: CSignal
        onCommunicationStatus:{
            if(status==0 && isClicked){
                isClicked=false
                connToast.show("连接成功", 0)
                readip.enabled=true
                setid.enabled=true
            }
            if(status==0){
                readip.enabled=true
                setid.enabled=true
            }

            else if(status==1 && isClicked){
                connToast.show("连接失败", 1)
            }
        }
        onEnergySpectrumStatus: {
            if(status==0 && isClicked2){
                isClicked=false
                connToast.show("连接成功", 0)
            }
            else if(status==1 && isClicked2){
                connToast.show("连接失败", 1)
            }
        }
        onSendConnectInfo:{
            ip.setText(ipv)
            port.setText(portv)
        }
    }
    Component.onCompleted: {
        var buttonList = row_button.children          // 初始化按钮样式，TCP删除了所以不用了
        for(let i = 0; i < buttonList.length; i ++){
            buttonList[i].button_color = "transparent"
            buttonList[i].button_color_select = "white"
            buttonList[i].radius = 4*dp
        }
        refreshInfoFromDB()

    }

    function refreshInfoFromDB(){

        connectType = CGetMessageInterface.getDefaultConnectInfo()
        var data=CGetMessageInterface.getConnectInfo(connectType);
        ip.setText(data["Ip"])
        port.setText(data["Port"])
        comId.model = CGetMessageInterface.getSerialPort()
    }

    onVisibleChanged: {
        if(visible){
            refreshInfoFromDB()
        }
    }

    Toast{
        id: connToast
        y: 150*dp
        x: parent.width / 2 - 150*dp
        z: 100
    }

    // TAB按钮 ================================================
    Rectangle{
        visible: false
        color: "#EBEEF3"
        anchors.horizontalCenter: parent.horizontalCenter
        y: 60*dp
        height: 54*dp; width: 224*dp
        radius: 4*dp
        Row{
            id: row_button
            x: 2*dp; y: 2*dp
            PushButton{
                title: "UDP通信"
                width: 110*dp; height: 50*dp
                isSelected: connectType === 0
                onReleased: {
                    connectType = 0
                    var data=CGetMessageInterface.getConnectInfo(0);
                    ip.setText(data["Ip"])
                    port.setText(data["Port"])
                }
            }
            PushButton{
                title: "能谱检测"
                width: 110*dp; height: 50*dp
                isSelected: connectType === 1
                onReleased: {
                    connectType = 1
                }
            }
            //            PushButton{
            //                title: "TCP通信"
            //                width: 110*dp; height: 50*dp
            //                isSelected: connectType === 1
            //                onReleased: {
            //                    connectType = 1
            //                    var data=GetMessageInterface.getConnectInfo(1);
            //                    ip.setText(data["Ip"])
            //                    port.setText(data["Port"])
            //                }
            //            }
            //            PushButton{
            //                title: "USB通信"
            //                width: 110*dp; height: 50*dp
            //                isSelected: connectType === 2
            //                onReleased: {
            //                    connectType = 2
            //                    var data = GetMessageInterface.getConnectInfo(2);
            //                    ip.setText(data["Ip"])
            //                    port.setText(data["Port"])
            //                }
            //            }
        }

    }
    // 文本框 =============================================
    Item{
        y: 140*dp
        anchors.horizontalCenter: parent.horizontalCenter
        width: 440*dp; height: parent.height-140*dp
        // 端口号页面 ================================================
        Column{
            visible: connectType === 1
            anchors.fill: parent
            spacing: 20*dp
            CustomComboBox{
                id: comId
                editWidth: 380*dp
                titleWidth: 60*dp
                title: "端口号"
                onPopupVisibleChanged: {
                    model = CGetMessageInterface.getSerialPort()
                }
                model: ["1"]
            }
            Rectangle { height: 210*dp; width: 20*dp }
            PushButton{
                title: "连接"
                anchors.right: parent.right
                defaultMode1: true
                onReleased: {
                    isClicked2 = true
                    CGetMessageInterface.connectEnergySpectrum(comId.getText())
                }
            }
        }
        // IP页面 ====================================================
        Column{
            visible: connectType === 0
            anchors.fill: parent
            spacing: 20*dp
            CustomInputText{
                id: ip
                required: true
                title: "IP地址"
                defaultText: "127.0.0.1"
                editWidth: 380*dp
                titleWidth: 60*dp
                ipflag: true
                Keys.onTabPressed:{
                    port.forceActiveFocus()
                }
            }
            CustomInputText{
                id: port
                required: true
                title: "端口号"
                defaultText: "8999"
                editWidth: 380*dp
                titleWidth: 60*dp
                intflag: true
                inputMaxLength: 5
                Keys.onTabPressed:{
                    ip.forceActiveFocus()
                }
            }
            Text {
                width: 440*dp  // 设置宽度以触发换行
                wrapMode: Text.WordWrap  // 开启自动换行
                text: qsTr("<font color='red'>*</font>注:默认为UDP通讯，读取按钮获取设备当前的IP和端口号，修改后点击写入按钮配置设备的IP和端口号，配置成功后重新连接。")
            }
            Rectangle { height: 100*dp; width: 20*dp }
            Rectangle{
                height: 40*dp
                x: parent.width-380*dp
                width: 380*dp
                CustomButton{
                    id:readip
                    x:0*dp
                    title: "读取"
                    defaultMode1: true
                    enabled: false
                    onReleased: {
                        CGetMessageInterface.readOrWriteConnectInfo(2,ip.getText(), port.getText())
                    }
                }
                CustomButton{
                    id:setid
                    x:130*dp
                    title: "写入"
                    defaultMode1: true
                    enabled: false
                    onReleased: {
                        CGetMessageInterface.readOrWriteConnectInfo(1,ip.getText(), port.getText())
                    }
                }
                PushButton{
                    x:260*dp
                    title: "连接"
                    defaultMode1: true
                    onReleased: {
                        isClicked=true
                        CGetMessageInterface.connectEqu(connectType,ip.getText(), port.getText())
                    }
                }
            }
        }
    }
}
